<template>
  <div>
    <!-- 轮播图 -->
    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
      <van-swipe-item v-for="item in gallery" :key="item.id">
        <img :src="item.img_url" alt="" width="100%" style="block" />
      </van-swipe-item>
    </van-swipe>
    <!-- 标签 -->
    <Tips></Tips>
    <!-- 弹出框 -->
    <div class="info">
      <h3>{{ info.name }}</h3>

      <p>{{ info.goods_brief }}</p>

      <div class="price">{{ info.retail_price | RMBformat }}</div>
    </div>
    <van-cell title="展示弹出层" is-link @click="isShow = !isShow" />
    <!-- 商品参数 -->
    <div class="attribute">
      <h3>商品参数</h3>
      <ul>
        <li v-for="(item, key) in attribute" :key="key">
          <div class="left">{{ item.name }}</div>
          <div class="right van-ellipsis">{{ item.value }}</div>
        </li>
      </ul>
    </div>
    <!-- 商品图片信息 -->
    <div class="goods_desc" v-html="goods_desc"></div>

    <!-- 常见问题 -->
    <div class="title">
      <span>常见问题</span>
    </div>
    <ul class="issue">
      <li v-for="item in issue" :key="item.id">
        <h3>{{ item.question }}</h3>
        <p>{{ item.answer }}</p>
      </li>
    </ul>
    <!-- 大家都在看 -->
    <div class="title">
      <span>大家都在看</span>
    </div>
    <div class="products">
      <Products :GoodLists="goodsList"></Products>
    </div>

    <!-- GoodsActive -->
    <AppGoodsActive
      @addToCart="addToCart"
      :goodsCount="goodsCount"
    ></AppGoodsActive>
    <!-- 弹出框 -->
    <van-sku
      ref="sku"
      v-model="isShow"
      :sku="sku"
      :goods="goods"
      :hide-stock="sku.hide_stock"
    />
  </div>
</template>

<script>
import { GetGoodsDetail, GetGoodsRelated,GetCartGoodsCount ,CartAdd} from "@/request/api";
import Tips from "@/components/Tips";
import Products from "@/components/Products";
import AppGoodsActive from "@/components/AppGoodsActive";
export default {
  data() {
    return {
      gallery: [], //轮播图数据
      info: {}, //商品信息
      attribute: [], //商品参数
      goods_desc: "",//商品描述
      issue: [],//常见问题
      goodsList: [],//相关产品
      goodsCount:0,//商品数量
      productList:[],//添加购物车的产品列表
      selectednum:0,
      // -----------------sku属性
      isShow: false,
      sku: {
        tree: [],
        hide_stock: false,
        price: "",
        stock_num: 227,
      },
      goods: {
        // 默认商品 sku 缩略图
        picture: "",
      },
    };
  },
  created() {
    //发送请求 ，请求该产品的数据
    let id = this.$route.query.id;

    GetGoodsDetail({ id }).then((res) => {
      console.log(res.data);
      this.gallery = res.data.gallery;
      this.info = res.data.info;
      this.attribute = res.data.attribute;
      this.goods_desc = res.data.info.goods_desc;
      this.issue = res.data.issue;
      this.goods.picture = res.data.info.list_pic_url;
      this.sku.price = res.data.info.retail_price;
      this.sku.stock_num = res.data.info.goods_number;
      this.productList = res.data.productList
    });

    GetGoodsRelated({ id:this.$route.query.id }).then((res) => {
      this.goodsList = res.data.goodsList;
    });

    GetCartGoodsCount({}).then((res)=>{
      this.goodsCount = res.data.cartTotal.goodsCount
    })

    
  },
  components: {
    Tips,
    AppGoodsActive,
    Products,
  },
  methods:{
    addToCart(){
      if(this.isShow){
        //执行加入购物车
        // let selectednum =  this.$refs.sku.getSkuData().selectedNum;
        // this.selectednum = selectednum;
        CartAdd({
      goodsId:this.$route.query.id,
      productId:this.productList[0].id,
      number:this.$refs.sku.getSkuData().selectedNum
          }).then((res) =>{
                    //1、更新购物车数量 
                    console.log(res.data.cartTotal.goodsCount); 
                    this.goodsCount = res.data.cartTotal.goodsCount
                   //2、跳转到购物车页面 
                    this.$toast.success("添加成功")
                    setTimeout(()=>{
                        this.$router.push("/cart")
                    },1500)
                    console.log(res);
    })
      }else{
        this.isShow = true
      }
    }
  }
};
</script>
 
<style lang = "less" scoped>
.info {
  background: #fff;
  text-align: center;
  padding: 0.2rem 0;
  border-bottom: 1px solid #efefee;
  h3 {
    font-weight: normal;
    font-size: 0.2rem;
    line-height: 0.5rem;
  }
  p {
    color: #999;
    font-size: 0.14rem;
    line-height: 0.3rem;
  }
  div {
    color: darkred;
    font-size: 0.14rem;
    line-height: 0.3rem;
  }
}
.attribute {
  background-color: #fff;
  margin-top: 0.2rem;
  padding: 0.1rem 0.168rem;
  h3 {
    line-height: 0.5rem;
    text-align: center;
    font-size: 0.1683rem;
  }
  li {
    height: 40px;
    line-height: 40px;
    background-color: #efefef;
    display: flex;
    margin: 0.1rem 0;
    border-radius: 0.1rem;
    .left {
      width: 30%;
      flex: 3;
      text-align: right;
      margin-right: 0.1rem;
    }
    .right {
      flex: 7;
      margin-left: 0.1rem;
    }
  }
}
/* 深度样式写法，穿透ui组件样式，直接添加在标签上 */
/deep/.goods_desc {
  img {
    width: 100%;
    display: block;
  }
}
.title {
  width: 100%;
  background: #fff;
  height: 0.5rem;
  position: relative;
  &::before {
    content: "";
    width: 50%;
    height: 2px;
    background: #ccc;
    position: absolute;
    top: 50%;
    margin-top: -1px;
    left: 50%;
    margin-left: -25%;
  }
  span {
    width: 30%;
    position: relative;
    background: #fff;
    display: block;
    text-align: center;
    margin: 0 auto;
    height: 0.5rem;
    line-height: 0.5rem;
  }
}
.issue {
  padding: 0 4%;
  background: #fff;
  li {
    h3 {
      height: 0.3rem;
      line-height: 0.3rem;
      padding-left: 0.2rem;
      position: relative;
      font-weight: normal;
      &::before {
        content: "";
        width: 4px;
        height: 4px;
        background: darkred;
        display: inline-block;
        position: absolute;
        left: 0;
        top: 50%;
        margin-top: -2px;
      }
    }
    p {
      font-size: 0.12rem;
      line-height: 0.2rem;
      padding-left: 0.2rem;
    }
  }
}
.products {
  padding-bottom: 0.6rem;
  background-color: #fff;
}
</style>